﻿Imports ADOEntity

Partial Class Administrator_userRightEdit
    Inherits Web.UI.BasePage

    Dim sUserRightEntity As userEntities
    Dim strMsg As String = ""
    Dim clsSessionMenuRight As clsSessionMenuRight
#Region " Even Page"
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
       

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim strStatus As String = Request.QueryString("Status")
            Dim strUserRight As String = Request.QueryString("strUserRight")
            sUserRightEntity = New UserEntities
            clsSessionMenuRight = Session("ClsMenuRight")
            If Not (IsPostBack) Then
                CreateControlMP()
               

                If (strStatus = "Add") Then
                    setDeptCode()

                    Dim dept = From dp In sUserRightEntity.SDEPARTMENT _
                               Group Join tblAll In (From ur In sUserRightEntity.SUSERRIGHT Join _
                                                        urd In sUserRightEntity.USERRIGHTDEPT _
                                                            On ur.USERRIGHT Equals urd.USERRIGHT _
                                                     Where ur.USERRIGHT = "" _
                                                     Select ur.USERRIGHT, ur.USERRIGHTDESC, urd.DEPTCODE) _
                                                 On dp.DEPTCODE Equals tblAll.DEPTCODE Into tblUseAll = Group
                               From tblUserRight In tblUseAll.DefaultIfEmpty _
                               Order By dp.DEPTCODE Ascending _
                               Select dp.DEPTCODE, dp.DEPTNAME, DeptIsUse = tblUserRight.DEPTCODE

                    If (clsSessionMenuRight.getRoleGroup < 80) Then
                        dept = dept.Where(Function(fn) fn.DEPTCODE = clsSessionMenuRight.getDeptCode)
                    End If
                    rdolStatus.SelectedValue = 1

                    grdDepartment.DataSource = dept.ToList
                    grdDepartment.DataBind()

                    If (grdDepartment.Rows.Count = 1) Then
                        Dim chkTemp As CheckBox
                        chkTemp = CType(grdDepartment.Rows(0).Cells(2).FindControl("chkDept"), CheckBox)
                        chkTemp.Checked = True
                    End If
                    chkRoleEnable(clsSessionMenuRight.getRoleGroup, strStatus)


                    setButtonAddOrEdit(strStatus)
                ElseIf (strStatus = "Edit") Then

                    sUserRightEntity = New UserEntities
                    Dim qry = (From UserRight In sUserRightEntity.SUSERRIGHT Where UserRight.USERRIGHT = strUserRight).ToList


                    Dim dept = From dp In sUserRightEntity.SDEPARTMENT _
                               Group Join tblAll In (From ur In sUserRightEntity.SUSERRIGHT Join urd In sUserRightEntity.USERRIGHTDEPT _
                                                            On ur.USERRIGHT Equals urd.USERRIGHT _
                                                     Where ur.USERRIGHT = strUserRight.Trim _
                                                     Select ur.USERRIGHT, ur.USERRIGHTDESC, urd.DEPTCODE) _
                                                 On dp.DEPTCODE Equals tblAll.DEPTCODE Into tblUseAll = Group
                               From tblUserRight In tblUseAll.DefaultIfEmpty
                                Order By dp.DEPTCODE Ascending _
                                Select dp.DEPTCODE, dp.DEPTNAME, DeptIsUse = tblUserRight.DEPTCODE

                    If (clsSessionMenuRight.getRoleGroup < 80) Then
                        dept = dept.Where(Function(fn) fn.DEPTCODE = clsSessionMenuRight.getDeptCode)
                    End If

                    setDeptCode(qry(0).USERRIGHT)
                    'txtUserRight.Text = qry(0).USERRIGHT
                    txtUserRightDesc.Text = qry(0).USERRIGHTDESC
                    If (qry(0).ROLEGHB = 1) Then
                        chkRoleGHB.Checked = True
                    End If
                    If (qry(0).ROLESYS = 1) Then
                        ChkRoleSys.Checked = True
                    End If
                    If (qry(0).ROLEDEPT = 1) Then
                        chkRoleDept.Checked = True
                    End If
                    If (qry(0).ROLEZONE = 1) Then
                        chkRoleZone.Checked = True
                    End If
                    If (qry(0).ROLEBRANCH = 1) Then
                        chkRoleBranch.Checked = True
                    End If
                    If (qry(0).ROLEUSER = 1) Then
                        chkRoleUser.Checked = True
                    End If
                    rdolStatus.SelectedValue = qry(0).STATUS
                    grdDepartment.DataSource = dept.ToList
                    grdDepartment.DataBind()


                    If (strUserRight = clsSessionMenuRight.getUserRight) Then
                        If (clsSessionMenuRight.getRoleGroup > 79) Then
                            enableControl(True)
                            chkRoleEnable(clsSessionMenuRight.getRoleGroup, strStatus)
                        Else
                            chkRoleEnable(clsSessionMenuRight.getRoleGroup, strStatus)
                            enableControl(False)
                        End If

                    Else
                        enableControl(True)
                        chkRoleEnable(clsSessionMenuRight.getRoleGroup, strStatus)
                    End If
                    setButtonAddOrEdit(strStatus, True)

                End If
                'If Not (clsSessionMenuRight.getRoleGroup > 79) Then
                '    lblUserRightStatus.Visible = False
                '    rdolStatus.Visible = False

                'End If
                ChkPageStatus(strStatus)
            End If
           

        Catch ex As Exception

        End Try
    End Sub
#End Region

    Private Sub enableControl(ByVal chk As Boolean)
        txtUserRight.Enabled = chk
        txtUserRightDesc.Enabled = chk
        rdolStatus.Enabled = chk
        chkRoleGHB.Enabled = chk
        ChkRoleSys.Enabled = chk
        chkRoleDept.Enabled = chk
        chkRoleZone.Enabled = chk
        chkRoleBranch.Enabled = chk
        chkRoleUser.Enabled = chk
        grdDepartment.Enabled = chk
        btnOK.Enabled = chk
        If (chk = False) Then
            btnSetUserRight.Text = "ตรวจสอบสิทธิ์การใช้งาน"
        End If

        '
    End Sub

#Region "SetForm"

 
    Private Sub setDeptCode(Optional ByVal strUserRight As String = "")
        Try
            chkIsLogin()
            Dim strDept As String = ""
            If (strUserRight = "") Then
                strDept = clsSessionMenuRight.getDeptCode
                strDept = strDept.Trim & "-"

                txtUserRightDeptCode.Text = strDept
                txtUserRight.MaxLength = 20 - strDept.Length
            Else
                Dim strSplit() As String = Nothing
                strDept = strUserRight
                strSplit = strDept.Split("-")
                If (strSplit.Length = 1) Then
                    txtUserRight.Text = strSplit(0)
                    txtUserRightDeptCode.Text = ""
                Else
                    txtUserRightDeptCode.Text = strSplit(0) & "-"
                    txtUserRight.Text = strSplit(1)
                    txtUserRight.MaxLength = 20 - txtUserRightDeptCode.Text.Length
                End If
            End If
        Catch ex As Exception
            WriteError(ex, "setDeptCode", "userRightEdit")
        End Try


        'If (clsSessionMenuRight.getRoleGroup > 79) Then
        '    txtUserRightDeptCode.Enabled = True
        'Else
        '    txtUserRightDeptCode.Enabled = False
        'End If
        'Dim strDept As String = ""
        'strDept = clsSessionMenuRight.getDeptCode
        'strDept = strDept.Trim & "-"
        'txtUserRight.Text = strDept

    End Sub


    Private Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "กลุ่มสิทธิ์"
    End Sub

    Private Sub setButtonAddOrEdit(ByVal strStatus As String, Optional ByVal blnVisible As Boolean = False)
        If (strStatus = "Add") Then
            btnSetUserRight.Text = "ตั้งค่าสิทธิ์การใช้งาน"
            btnSetUserRight.Visible = blnVisible
            'btnCancel.Text = "กลับหน้าแรก"
        ElseIf (strStatus = "Edit") Then
            btnSetUserRight.Text = "แก้ไขตั้งค่าสิทธิ์การใช้งาน"
            btnSetUserRight.Visible = blnVisible
            'btnCancel.Text = "กลับหน้าแรก"
        End If

    End Sub

#End Region

#Region " Even Button "
    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim chkPassSave As Int16 = 0
        If chkBeforeSave() Then
            sUserRightEntity = New UserEntities
            Dim sUserRight As New SUSERRIGHT
            Dim userDept As USERRIGHTDEPT
            sUserRightEntity.Connection.Open()
            Using transaction As System.Data.Common.DbTransaction = sUserRightEntity.Connection.BeginTransaction()
                Try
                    Dim strAllCodeUserRight As String = txtUserRightDeptCode.Text & txtUserRight.Text


                    If (Request.QueryString("Status") = "Add" And txtUserRight.Enabled = True) Then

                        sUserRight.USERRIGHT = strAllCodeUserRight
                        sUserRight.USERRIGHTDESC = txtUserRightDesc.Text
                        sUserRight.ROLEGHB = If(chkRoleGHB.Checked = True, 1, 0)
                        sUserRight.ROLESYS = If(ChkRoleSys.Checked = True, 1, 0)
                        sUserRight.ROLEDEPT = If(chkRoleDept.Checked = True, 1, 0)
                        sUserRight.ROLEZONE = If(chkRoleZone.Checked = True, 1, 0)
                        sUserRight.ROLEBRANCH = If(chkRoleBranch.Checked = True, 1, 0)
                        sUserRight.ROLEUSER = If(chkRoleUser.Checked = True, 1, 0)
                        sUserRight.STATUS = rdolStatus.SelectedValue
                        sUserRightEntity.AddToSUSERRIGHT(sUserRight)

                        Dim icount = grdDepartment.Rows.Count
                        Dim chkTemp As CheckBox
                        For i As Integer = 0 To icount - 1
                            chkTemp = CType(grdDepartment.Rows(i).Cells(2).FindControl("chkDept"), CheckBox)
                            If (chkTemp.Checked) Then
                                userDept = New USERRIGHTDEPT
                                userDept.USERRIGHT = strAllCodeUserRight
                                userDept.DEPTCODE = grdDepartment.Rows(i).Cells(0).Text
                                sUserRightEntity.AddToUSERRIGHTDEPT(userDept)
                            End If
                        Next

                        SaveLogNew()

                    ElseIf (Request.QueryString("Status") = "Edit" Or txtUserRight.Enabled = False) Then
                        Dim qry = (From UserRight In sUserRightEntity.SUSERRIGHT Where UserRight.USERRIGHT = strAllCodeUserRight).ToList

                        qry(0).USERRIGHTDESC = txtUserRightDesc.Text
                        qry(0).ROLEGHB = If(chkRoleGHB.Checked = True, 1, 0)
                        qry(0).ROLESYS = If(ChkRoleSys.Checked = True, 1, 0)
                        qry(0).ROLEDEPT = If(chkRoleDept.Checked = True, 1, 0)
                        qry(0).ROLEZONE = If(chkRoleZone.Checked = True, 1, 0)
                        qry(0).ROLEBRANCH = If(chkRoleBranch.Checked = True, 1, 0)
                        qry(0).ROLEUSER = If(chkRoleUser.Checked = True, 1, 0)
                        qry(0).STATUS = rdolStatus.SelectedValue

                        Dim qryUrDept = (From userRightDept In sUserRightEntity.USERRIGHTDEPT Where userRightDept.USERRIGHT = strAllCodeUserRight).ToList
                        Dim icountTbl As Integer = qryUrDept.Count
                        For i As Integer = 0 To icountTbl - 1
                            sUserRightEntity.DeleteObject(qryUrDept(i))
                            sUserRightEntity.SaveChanges()
                        Next



                        icountTbl = grdDepartment.Rows.Count
                        Dim chkTemp As CheckBox

                        For i As Integer = 0 To icountTbl - 1
                            chkTemp = CType(grdDepartment.Rows(i).Cells(2).FindControl("chkDept"), CheckBox)
                            If (chkTemp.Checked) Then
                                userDept = New USERRIGHTDEPT
                                userDept.USERRIGHT = strAllCodeUserRight
                                userDept.DEPTCODE = grdDepartment.Rows(i).Cells(0).Text
                                sUserRightEntity.AddToUSERRIGHTDEPT(userDept)
                                sUserRightEntity.SaveChanges()
                            End If
                        Next


                        SaveLogEdit()

                    End If

                    sUserRightEntity.SaveChanges()
                    transaction.Commit()
                    chkPassSave = 1

                Catch ex As System.Data.UpdateException

                    Dim codeError() As String = ex.InnerException.Message.Split(":")
                    If codeError(0) = "ORA-00001" Then
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('ไม่สามารถเพิ่มได้ เนื่องจากรหัสนี้มีการนำมาใช้ในระบบแล้ว')", True)
                    Else
                        WriteError(ex, "btnOK_Click", "userRightEdit")
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + ex.InnerException.Message + "')", True)
                    End If
                    transaction.Rollback()
                    chkPassSave = 0
                Catch ex As Exception
                    WriteError(ex, "btnOK_Click", "userRightEdit")
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('บันทึกผิดพลาด')", True)
                    transaction.Rollback()
                    chkPassSave = 0
                End Try
            End Using



            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + Resources.resMsgKYC.msgSave + "')", True)
        Else
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
            chkPassSave = 0
        End If
        If (chkPassSave = 1) Then

            txtUserRight.Enabled = False
            setButtonAddOrEdit(Request.QueryString("Status"), True)
        End If

    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("UserRightList.aspx")
    End Sub
#End Region

#Region " Function "

    Private Sub chkRoleEnable(ByVal strRoleCode As String, ByVal strStatus As String)
        If (strStatus = "Add") Then
            If (strRoleCode > 89) Then
                chkRoleGHB.Enabled = True
                ChkRoleSys.Enabled = True
                chkRoleDept.Enabled = True
                chkRoleZone.Enabled = True
                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True

            ElseIf (strRoleCode > 79) Then
                chkRoleGHB.Enabled = False
                chkRoleGHB.Checked = True

                ChkRoleSys.Enabled = True
                chkRoleDept.Enabled = True
                chkRoleZone.Enabled = True
                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True
            ElseIf (strRoleCode > 69) Then
                chkRoleGHB.Enabled = False
                ChkRoleSys.Enabled = False
                chkRoleDept.Enabled = False
                chkRoleGHB.Checked = True
                ChkRoleSys.Checked = True
                chkRoleDept.Checked = True

                chkRoleZone.Enabled = True
                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True
            ElseIf (strRoleCode > 59) Then

                chkRoleGHB.Enabled = False
                ChkRoleSys.Enabled = False
                chkRoleDept.Enabled = False
                chkRoleZone.Enabled = False
                chkRoleGHB.Checked = True
                ChkRoleSys.Checked = True
                chkRoleDept.Checked = True
                chkRoleZone.Checked = True

                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True
            Else
                chkRoleGHB.Enabled = False
                ChkRoleSys.Enabled = False
                chkRoleDept.Enabled = False
                chkRoleZone.Enabled = False
                chkRoleBranch.Enabled = False
                chkRoleGHB.Checked = True
                ChkRoleSys.Checked = True
                chkRoleDept.Checked = True
                chkRoleZone.Checked = True
                chkRoleBranch.Checked = True

                chkRoleUser.Enabled = True
            End If
        ElseIf (strStatus = "Edit") Then

            If (strRoleCode > 89) Then
                chkRoleGHB.Enabled = True
                ChkRoleSys.Enabled = True
                chkRoleDept.Enabled = True
                chkRoleZone.Enabled = True
                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True

            ElseIf (strRoleCode > 79) Then
                chkRoleGHB.Enabled = False
                ChkRoleSys.Enabled = True
                chkRoleDept.Enabled = True
                chkRoleZone.Enabled = True
                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True
            ElseIf (strRoleCode > 69) Then
                chkRoleGHB.Enabled = False
                ChkRoleSys.Enabled = False
                chkRoleDept.Enabled = False
                chkRoleZone.Enabled = True
                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True
            ElseIf (strRoleCode > 59) Then
                chkRoleGHB.Enabled = False
                ChkRoleSys.Enabled = False
                chkRoleDept.Enabled = False
                chkRoleZone.Enabled = False
                chkRoleBranch.Enabled = True
                chkRoleUser.Enabled = True
            Else
                chkRoleGHB.Enabled = False
                ChkRoleSys.Enabled = False
                chkRoleDept.Enabled = False
                chkRoleZone.Enabled = False
                chkRoleBranch.Enabled = False
                chkRoleUser.Enabled = True
            End If
        End If






    End Sub

    Private Sub ChkPageStatus(ByVal strStatus As String)
        If Not (strStatus = "Add") Then
            txtUserRight.Enabled = False
            txtUserRightDeptCode.Enabled = False
        End If
    End Sub

    Private Function chkBeforeSave() As Boolean
        Dim result As Boolean = True
        Dim intTestInteger As Double
        If (txtUserRight.Text = "") Then
            result = False
            strMsg = strMsg & "กรุณาระบุรหัสกลุ่มสิทธิ์" & "\n"

        Else
            Try
                Dim strUserRightRep As String = ""
                strUserRightRep = txtUserRight.Text
                intTestInteger = strUserRightRep
            Catch ex As Exception
                strMsg = strMsg & "กรุณาระบุรหัสกลุ่มสิทธิ์เป็นตัวเลข" & "\n"
                result = False
            End Try
        End If
        If (txtUserRightDesc.Text = "") Then
            result = False
            strMsg = strMsg & "กรุณาระบุชื่อผู้ใช้งาน" & "\n"
        End If

        Dim iCountChk As Integer = 0

        If (chkRoleGHB.Checked = True) Then
            iCountChk += 1
        End If
        If (ChkRoleSys.Checked = True) Then
            iCountChk += 1
        End If
        If (chkRoleDept.Checked = True) Then
            iCountChk += 1
        End If
        If (chkRoleZone.Checked = True) Then
            iCountChk += 1
        End If
        If (chkRoleBranch.Checked = True) Then
            iCountChk += 1
        End If
        If (chkRoleUser.Checked = True) Then
            iCountChk += 1
        End If

        If (iCountChk = 0) Then
            result = False
            strMsg = strMsg & "กรุณาระบุสิทธิ์การเข้าถึงข้อมูล อย่างน้อย 1 สิทธิ์" & "\n"
        End If

        Dim chkCount As Integer = 0

        Dim icount = grdDepartment.Rows.Count
        Dim chkTemp As CheckBox
        For i As Integer = 0 To icount - 1
            chkTemp = CType(grdDepartment.Rows(i).Cells(2).FindControl("chkDept"), CheckBox)
            If (chkTemp.Checked) Then
                chkCount += 1
            End If
        Next
        If chkCount = 0 Then
            result = False
            strMsg = strMsg & "กรุณาระบุฝ่ายอย่างน้อย 1 ฝ่าย" & "\n"
        End If

        Return result

    End Function

    Private Sub SaveLogNew()
        Try

            Dim desc As String = "บันทึก ข้อมูลกลุ่มสิทธิ์ (รหัสกลุ่มสิทธิ์ " + txtUserRightDeptCode.Text + txtUserRight.Text + ")"

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction(txtUserRightDeptCode.Text + "-" + txtUserRight.Text, clsSessionMenuRight.getUserName, 3, desc, "M62")
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub SaveLogEdit()
        Try

            Dim desc As String = "บันทึก แก้ไข ข้อมูลกลุ่มสิทธิ์ (รหัสกลุ่มสิทธิ์ " + txtUserRightDeptCode.Text + txtUserRight.Text + ")"

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction(txtUserRightDeptCode.Text + "-" + txtUserRight.Text, clsSessionMenuRight.getUserName, 4, desc, "M62")
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

    Protected Sub btnSetUserRight_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSetUserRight.Click
        Dim strAllCodeUserRight As String = txtUserRightDeptCode.Text & txtUserRight.Text
        Response.Redirect("MenuRight.aspx?userRight=" & strAllCodeUserRight)
    End Sub
End Class
